// messageboxutil.cpp

#include "messageboxutil.h"

MessageBoxUtil::MessageBoxUtil(QObject *parent) : QObject(parent)
{
}

int MessageBoxUtil::showMessage(QWidget *parent, MessageBoxUtil::MessageBoxType type, const QString &title, const QString &text, MessageBoxUtil::ButtonTypes buttons)
{
    QMessageBox msgBox(parent);
    msgBox.setWindowTitle(title);
    msgBox.setText(text);

    // 设置消息框图标
    switch (type) {
    case Question:
        msgBox.setIcon(QMessageBox::Question);
        break;
    case Information:
        msgBox.setIcon(QMessageBox::Information);
        break;
    case Warning:
        msgBox.setIcon(QMessageBox::Warning);
        break;
    case Critical:
        msgBox.setIcon(QMessageBox::Critical);
        break;
    }

    // 设置按钮
    QMessageBox::StandardButtons standardButtons;
    for (int i = Ok; i <= Ignore; i <<= 1) {
        if (buttons & i) {
            ButtonType button = static_cast<ButtonType>(i);
            QMessageBox::StandardButton standardButton = convertToStandardButton(button);
            standardButtons |= standardButton;
            msgBox.addButton(standardButton);
            msgBox.setButtonText(standardButton, getButtonText(button));
        }
    }
    msgBox.setStandardButtons(standardButtons);

    // 执行消息框并返回结果
    return msgBox.exec();
}

QMessageBox::StandardButton MessageBoxUtil::convertToStandardButton(MessageBoxUtil::ButtonType button)
{
    switch (button) {
    case Ok: return QMessageBox::Ok;
    case Cancel: return QMessageBox::Cancel;
    case Yes: return QMessageBox::Yes;
    case No: return QMessageBox::No;
    case Abort: return QMessageBox::Abort;
    case Retry: return QMessageBox::Retry;
    case Ignore: return QMessageBox::Ignore;
    default: return QMessageBox::NoButton;
    }
}

QString MessageBoxUtil::getButtonText(MessageBoxUtil::ButtonType button)
{
    switch (button) {
    case Ok: return "确定";
    case Cancel: return "取消";
    case Yes: return "是";
    case No: return "否";
    case Abort: return "中止";
    case Retry: return "重试";
    case Ignore: return "忽略";
    default: return QString();
    }
}
